package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.JyjcSharingExpensesSumExport;
import com.ruoyi.system.mapper.JyjcExpensesLsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JyjcSharingExpensesSumMapper;
import com.ruoyi.system.domain.JyjcSharingExpensesSum;
import com.ruoyi.system.service.IJyjcSharingExpensesSumService;

/**
 * 财务收入支出统计Service业务层处理
 * 
 * @author 石亚兵
 * @date 2025-07-12
 */
@Service
public class JyjcSharingExpensesSumServiceImpl implements IJyjcSharingExpensesSumService 
{
    private static final Logger log = LoggerFactory.getLogger(JyjcSharingExpensesSumServiceImpl.class);
    @Autowired
    private JyjcSharingExpensesSumMapper jyjcSharingExpensesSumMapper;

    @Autowired
    private JyjcExpensesLsMapper jyjcExpensesLsMapper;
    /**
     * 查询财务收入支出统计
     * 
     * @param deptname 财务收入支出统计主键
     * @return 财务收入支出统计
     */
    @Override
    public JyjcSharingExpensesSum selectJyjcSharingExpensesSumByDeptname(String deptname)
    {
        return jyjcSharingExpensesSumMapper.selectJyjcSharingExpensesSumByDeptname(deptname);
    }

    /**
     * 查询财务收入支出统计列表
     * 
     * @param jyjcSharingExpensesSum 财务收入支出统计
     * @return 财务收入支出统计
     */
    @Override
    public List<JyjcSharingExpensesSum> selectJyjcSharingExpensesSumList(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {
        return jyjcSharingExpensesSumMapper.selectJyjcSharingExpensesSumList(jyjcSharingExpensesSum);
    }

    @Override
    public List<JyjcSharingExpensesSum> alllist(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {
        jyjcSharingExpensesSumMapper.updateNoSharingExpenses(jyjcSharingExpensesSum);
        return jyjcSharingExpensesSumMapper.alllist(jyjcSharingExpensesSum);
    }

    @Override
    public List<JyjcSharingExpensesSum> deptlist(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {
        return jyjcSharingExpensesSumMapper.deptlist(jyjcSharingExpensesSum);
    }

    @Override
    public List<JyjcSharingExpensesSum> importalllist(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {
        return jyjcSharingExpensesSumMapper.importalllist(jyjcSharingExpensesSum);
    }

    @Override
    public List<JyjcSharingExpensesSum> importalllistsd(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {
        return jyjcSharingExpensesSumMapper.importalllistsd(jyjcSharingExpensesSum);
    }

    @Override
    public List<JyjcSharingExpensesSum> allsumlist(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {

        jyjcSharingExpensesSumMapper.deleteJyjcExpensesLs();
        jyjcSharingExpensesSumMapper.updateNoSharingExpenses(jyjcSharingExpensesSum);
        List<JyjcSharingExpensesSum> batchjyjcSharingExpensesSumList=new ArrayList<>();
        List<JyjcSharingExpensesSum> jyjcSharingExpensesSumList=jyjcSharingExpensesSumMapper.alllist(jyjcSharingExpensesSum);
        for( JyjcSharingExpensesSum jyjcSharingExpensesSum1: jyjcSharingExpensesSumList ){
            batchjyjcSharingExpensesSumList.add(jyjcSharingExpensesSum1);
        }
        if(batchjyjcSharingExpensesSumList.size()>0){
            jyjcSharingExpensesSumMapper.batchJyjcExpensesLs(batchjyjcSharingExpensesSumList);
        }

        return jyjcSharingExpensesSumMapper.allsumlist(jyjcSharingExpensesSum);
    }

    @Override
    public List<JyjcSharingExpensesSumExport> allsumlistExport(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {
        jyjcSharingExpensesSumMapper.deleteJyjcExpensesLs();
        jyjcSharingExpensesSumMapper.updateNoSharingExpenses(jyjcSharingExpensesSum);
        List<JyjcSharingExpensesSum> jyjcSharingExpensesSumList=jyjcSharingExpensesSumMapper.alllist(jyjcSharingExpensesSum);
        for( JyjcSharingExpensesSum jyjcSharingExpensesSum1: jyjcSharingExpensesSumList ){
            jyjcSharingExpensesSumMapper.insertJyjcExpensesLs(jyjcSharingExpensesSum1);
        }
        return jyjcSharingExpensesSumMapper.allsumlistExport(jyjcSharingExpensesSum);
    }

    /**
     * 新增财务收入支出统计
     * 
     * @param jyjcSharingExpensesSum 财务收入支出统计
     * @return 结果
     */
    @Override
    public int insertJyjcSharingExpensesSum(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {
        return jyjcSharingExpensesSumMapper.insertJyjcSharingExpensesSum(jyjcSharingExpensesSum);
    }

    /**
     * 修改财务收入支出统计
     * 
     * @param jyjcSharingExpensesSum 财务收入支出统计
     * @return 结果
     */
    @Override
    public int updateJyjcSharingExpensesSum(JyjcSharingExpensesSum jyjcSharingExpensesSum)
    {
        return jyjcSharingExpensesSumMapper.updateJyjcSharingExpensesSum(jyjcSharingExpensesSum);
    }


    @Override
    public String importJyjcSharingExpensesSum(List<JyjcSharingExpensesSum> jyjcSharingExpensesSumList, Boolean isUpdateSupport)
    {

        if (StringUtils.isNull(jyjcSharingExpensesSumList) || jyjcSharingExpensesSumList.size() == 0)
        {
            throw new ServiceException("导入汇总数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        jyjcSharingExpensesSumMapper.deleteJyjcSharingExpensesSumByYearYf(jyjcSharingExpensesSumList.get(1));
        for (JyjcSharingExpensesSum jyjcSharingExpensesSum : jyjcSharingExpensesSumList)
        {
            try
            {

                jyjcSharingExpensesSumMapper.insertJyjcSharingExpensesSum(jyjcSharingExpensesSum);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、 " + jyjcSharingExpensesSum.getYear()+"年"+jyjcSharingExpensesSum.getYf()+"月"+jyjcSharingExpensesSum.getJjkm()  + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + jyjcSharingExpensesSum.getYear()+"年"+jyjcSharingExpensesSum.getYf()+"月"+jyjcSharingExpensesSum.getJjkm()   + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
